fix(hooks): source nix profile in pre-push when nix not in PATH#62
Open
fix(hooks): source nix profile in pre-push when nix not in PATH#62
Conversation
Git hooks run in a restricted environment that does not inherit the user's shell profile. When nix is installed via the daemon (multi-user) or single-user mode, the nix binary is not on PATH unless the profile scripts are sourced explicitly. Add _source_nix_profile_if_not_in_path() that checks for nix before doing anything, then sources whichever profile script exists: 1. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh (multi-user) 2. ~/.nix-profile/etc/profile.d/nix.sh (single-user fallback) Without this, every git push required SKIP_HOOKS=1 to bypass the nix-dependent checks (statix, deadnix, nixfmt, bats).
…ypes on Ubuntu Git hooks, VS Code terminals, and systemd user units all inherit PATH from different ancestors. /etc/profile.d/nix.sh only covers login bash shells, leaving non-login bash and non-shell processes without nix. Three-layer fix via new home/modules/nix-path-bootstrap.nix: Layer 1 — home.sessionPath Adds nix binaries to ~/.profile. Covers bash/sh login shells (SSH, su -, desktop session managers that source /etc/profile). Layer 2 — ~/.config/environment.d/10-nix-path.conf Injected into the systemd user session environment at login. Every process started from the user session manager inherits this PATH: terminal emulators, VS Code, GUI apps, and all their children (including git hooks). $PATH is expanded by systemd from the inherited system environment. Layer 3 — programs.bash.initExtra Sources /etc/profile.d/nix.sh (or single-user fallback) in non-login interactive bash sessions — terminal emulators that start bash without --login skip /etc/profile.d entirely. Guard prevents double-sourcing when nix is already present. programs.bash.enable = true required so home-manager actually writes the initExtra content to ~/.bashrc (without enable the option is silently ignored).
61ea5bb to
a17ae27
Compare
6227bc7 to
b84e24d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Git hooks run in a restricted environment that does not inherit the user's shell profile. The
~/.githooks/pre-pushhook callsnix run nixpkgs#statix,nix run nixpkgs#deadnix,nix run nixpkgs#nixfmt-rfc-style, andnix shell nixpkgs#bats— butnixis not onPATHwhen git invokes hooks.Symptom: every
git pushon the dotfiles repo failed with:The workaround was
SKIP_HOOKS=1 git push, which bypasses all quality checks.Fix
Add
_source_nix_profile_if_not_in_path()at the top of the hook (before anynixinvocation):nixis already in PATH — no-op (fast path, zero cost)/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.shfor multi-user installations~/.nix-profile/etc/profile.d/nix.shfor single-user installationsset -efailure)Testing
Smoke-tested by running the hook with
env -i PATH=/usr/bin:/bin— nix was successfully found after sourcing and all checks (statix, deadnix, nixfmt, bats) ran to completion.Notes
.(source) lines are suppressed with# shellcheck source=/dev/nullsince the sourced paths are runtime-determinedreadonly REPO_ROOT=$(…)is out of scope for this fix